import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Manager {

	public static int EmpID;
	static Integer[] blueSection = { 31, 41, 42 };
	static Integer redSection[] = { 11, 21, 12 };
	static Integer yellowSection[] = { 22, 32, 23, 33 };
	static Integer whiteSection[] = { 13, 14, 24 };
	static Integer geySection[] = { 43, 34, 44 };
	private static ArrayList<String> resHeaders;
	private static ArrayList<String> empHeaders;
	private static ArrayList<String> menuHeaders;

	public static void manager(int empID) {
		GUI.window.setTitle("Manager Screen");
		EmpID = empID;
		GUI.bottomPanel = GUI.exitOnly();
		// state="serverMain"; //Will be changed
		managerMain();

		GUI.windowDone();

	}

	static void managerMain() {
		GUI.resetPanels();
		GUI.leftMenu.add(reservationsButton(EmpID));
		GUI.leftMenu.add(employeesButton(EmpID));
		GUI.leftMenu.add(menuItemsButton(EmpID));

		for (int i = 1; i < 5; i++) {
			for (int j = 1; j < 5; j++) {
				Integer table = j * 10 + i;
				if (Server.contains(table, blueSection))
					GUI.centerPanel.add(Hostess.newTable(table, 75, GUI.blue,
							Color.black));
				else if (Server.contains(table, redSection))
					GUI.centerPanel.add(Hostess.newTable(table, 75, Color.RED,
							Color.black));
				else if (Server.contains(table, yellowSection))
					GUI.centerPanel.add(Hostess.newTable(table, 75, GUI.yellow,
							Color.black));
				else if (Server.contains(table, whiteSection))
					GUI.centerPanel.add(Hostess.newTable(table, 75,
							Color.WHITE, Color.black));
				else
					GUI.centerPanel.add(Hostess.newTable(table, 75, Color.GRAY,
							Color.black));

			}
		}
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.bottomPanel.add(GUI.exitOnly());
	}

	static JButton reservationsButton(int empID2) {
		JButton button = GUI.newButton("Reservations", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.resetPanels();
				reservationsPanel(empID2);
			}
		});
		return button;
	}

	static void reservationsPanel(int empID2) {
		resHeaders = new ArrayList<String>();
		resHeaders.add("Date");
		resHeaders.add("Time");
		resHeaders.add("# In Party");
		resHeaders.add("Name");
		resHeaders.add("Phone Number");
		resHeaders.add("Notes");

		// test for data array (blank lines)
		ArrayList<String> blankTest = new ArrayList<String>();
		for (int i = 0; i < 61; i++) {
			blankTest.add("");
		}

		GUI.window.setTitle("Reservation List");
		GUI.centerPanel.setLayout(new BorderLayout());
		GUI.centerPanel.add(new Grids(resHeaders.size(), resHeaders, blankTest
				.size() / resHeaders.size(), blankTest)); // //// REPLACE LINE
															// 109 WITH THIS
															// GRID /////
		// GUI.centerPanel.add(new Label("Table "+tableNum.toString()));
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.leftMenu.add(addReservation(EmpID));
		GUI.leftMenu.add(editReservation(EmpID));
		GUI.leftMenu.add(removeReservation(EmpID));

		GUI.leftPanel.removeAll();
		GUI.leftPanel.add(GUI.leftMenu);
		GUI.bottomPanel.add(GUI.mainMenuBar(empID2));
	}

	private static Component removeReservation(int empID2) {
		JButton button = GUI.newButton("Remove Reservation", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// GUI.resetPanels();
				Grids.deleteResult(resHeaders);
			}
		});
		return button;
	}

	private static Component editReservation(int empID2) {
		JButton button = GUI.newButton("Edit Reservation", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// GUI.resetPanels();
				// Obtain the results from the highlighted row
				ArrayList<String> results = Grids.getEnteredResults();
				if (results != null) {

					ArrayList<String> modResults = PopUp
							.runReservation(results);
					// if a parsing error is found pass the arraylist back to
					// runReservation
					while (modResults != null
							&& modResults.get(modResults.size() - 1).equals(
									"ERROR")) {
						modResults = PopUp.runReservation(modResults);
					}
					if (modResults != null) {
						Grids.modifyResult(modResults);
					}
				}
			}
		});
		return button;
	}

	private static Component addReservation(int empID2) {
		JButton button = GUI.newButton("Add Reservation", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// A blank arraylist is used if a parsing error is found so
				// previous data
				// entered is not lost
				ArrayList<String> blank = new ArrayList<String>();
				for (int i = 0; i < resHeaders.size(); i++) {
					blank.add(null);
				}
				ArrayList<String> results = PopUp.runReservation(blank);
				// if a parsing error is found pass the arraylist back to
				// runManageEmployee
				while (results != null
						&& results.get(results.size() - 1).equals("ERROR")) {
					results = PopUp.runReservation(results);
				}
				if (results != null) {
					Grids.addNewResult(results, resHeaders);
				}
			}
		});
		return button;
	}

	static void menuItemsPanel(int empID2) {
		menuHeaders = new ArrayList<String>();
		menuHeaders.add("Item Name");
		menuHeaders.add("Item Price");
		menuHeaders.add("Item Activity");
		menuHeaders.add("Food/Drink");
		menuHeaders.add("Specific Category");

		// test for data array (blank lines)
		ArrayList<String> menuList = new ArrayList<String>();

		// Database call to get the menu information
		Connection c = null;
		ResultSet menuResults;
		try {
			c = SqliteHelper.getConn();

			Statement menuData = c.createStatement();
			String menuSelectQuery = "SELECT * FROM Menu;";
			menuResults = menuData.executeQuery(menuSelectQuery);

			while (menuResults.next()) {
				menuList.add(menuResults.getString("Item_Name"));
				menuList.add(menuResults.getString("Item_Price"));
				menuList.add(menuResults.getString("Item_Activity"));
				menuList.add(menuResults.getString("General_Cat"));
				menuList.add(menuResults.getString("Specific_Cat"));
			}

			GUI.window.setTitle("menuItem List");
			GUI.centerPanel.setLayout(new BorderLayout());
			GUI.centerPanel.add(new Grids(menuHeaders.size(), menuHeaders,
					menuList.size() / menuHeaders.size(), menuList)); // ////
																		// REPLACE
																		// LINE
																		// 109
																		// WITH
																		// THIS
																		// GRID
																		// /////
			// GUI.centerPanel.add(new Label("Table "+tableNum.toString()));
			GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
			GUI.leftMenu.add(addmenuItem(EmpID));
			GUI.leftMenu.add(editmenuItem(EmpID));
			GUI.leftMenu.add(removemenuItem(EmpID));

			GUI.leftPanel.removeAll();
			GUI.leftPanel.add(GUI.leftMenu);
			GUI.bottomPanel.add(GUI.mainMenuBar(empID2));
		} catch (Exception e) {
			System.err.println("Database Call Failure in menuItemsPanel.");
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
		}
		GUI.windowDone();
	}

	private static Component removemenuItem(int empID2) {
		JButton button = GUI.newButton("Remove menuItem", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// GUI.menuetPanels();
				ArrayList<String> results = Grids.getEnteredResults();
				String itemToRemove = results.get(0);

				// Delete from database
				Connection c = null;
				try {
					c = SqliteHelper.getConn();
					Statement deleteStatement = c.createStatement();
					String deleteQuery = "DELETE FROM Menu WHERE Item_Name = '"
							+ itemToRemove + "';";
					deleteStatement.execute(deleteQuery);
				} catch (Exception ex) {
					System.err
							.println("Database Call Failure in removeMenuItem.");
					System.err.println(ex.getClass().getName() + ": "
							+ ex.getMessage());
				}

				Grids.deleteResult(menuHeaders);
			}
		});
		return button;
	}

	private static Component editmenuItem(int empID2) {
		JButton button = GUI.newButton("Edit menuItem", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// GUI.menuetPanels();
				// Obtain the results from the highlighted row
				ArrayList<String> results = Grids.getEnteredResults();
				if (results != null) {
					// Obtain the item name for the update statement.
					String itemToModify = results.get(0);

					ArrayList<String> modresults = PopUp.runMenuItem(results);
					// if a parsing error is found pass the arraylist back to
					// runmenuItem
					while (modresults != null
							&& modresults.get(modresults.size() - 1).equals(
									"ERROR")) {
						modresults = PopUp.runMenuItem(modresults);
					}
					if (modresults != null) {
						// Run update on the database.
						Connection c = null;
						try {
							c = SqliteHelper.getConn();
							Statement updateStatement = c.createStatement();

							String updateQuery = "UPDATE Menu SET Item_Name = '"
									+ modresults.get(0)
									+ "', Item_Price = "
									+ modresults.get(1)
									+ ", Item_Activity = '"
									+ modresults.get(2)
									+ "', General_Cat = '"
									+ modresults.get(3)
									+ "', Specific_Cat = '"
									+ modresults.get(4)
									+ "' WHERE Item_Name = '"
									+ itemToModify
									+ "';";

							updateStatement.execute(updateQuery);
						} catch (Exception ex) {
							System.err
									.println("Database Call Failure in editMenuItem.");
							System.err.println(ex.getClass().getName() + ": "
									+ ex.getMessage());
						}

						Grids.modifyResult(modresults);
					}
				}
			}
		});
		return button;
	}

	private static Component addmenuItem(int empID2) {
		JButton button = GUI.newButton("Add menuItem", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// A blank arraylist is used if a parsing error is found so
				// previous data
				// entered is not lost
				ArrayList<String> blank = new ArrayList<String>();
				for (int i = 0; i < menuHeaders.size(); i++) {
					blank.add(null);
				}
				ArrayList<String> results = PopUp.runMenuItem(blank);
				// if a parsing error is found pass the arraylist back to
				// runManageEmployee
				while (results != null
						&& results.get(results.size() - 1).equals("ERROR")) {
					results = PopUp.runMenuItem(results);
				}
				if (results != null) {
					// Add the results to the database
					Connection c = null;
					try {
						c = SqliteHelper.getConn();
						Statement insertStatement = c.createStatement();

						// Inserts using the result arraylist for values. The
						// NULL is so the database will auto-increment the item
						// ID for the Primary Key.
						String insertQuery = "INSERT INTO Menu VALUES (NULL, '"
								+ results.get(0) + "', " + results.get(1)
								+ ", '" + results.get(2) + "', '"
								+ results.get(3) + "', '" + results.get(4) + "');";

						insertStatement.execute(insertQuery);
					} catch (Exception ex) {
						System.err
								.println("Database Call Failure in addMenuItem.");
						System.err.println(ex.getClass().getName() + ": "
								+ ex.getMessage());
					}

					Grids.addNewResult(results, menuHeaders);
				}
			}
		});
		return button;
	}

	static JButton employeesButton(int empID) {
		JButton button = GUI.newButton("Employees", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.resetPanels();
				employeesPanel(empID);
			}
		});
		return button;
	}

	protected static void employeesPanel(int empID2) {
		empHeaders = new ArrayList<String>();

		empHeaders.add("Name");
		empHeaders.add("Password");
		empHeaders.add("Hours/Week");
		empHeaders.add("Job Code");
		empHeaders.add("Notes");

		ArrayList<String> employeeList = new ArrayList<String>();
		// Database call to get the employee information
		Connection c = null;
		ResultSet employeeResults;
		try {
			c = SqliteHelper.getConn();

			Statement empSelectStatement = c.createStatement();
			String empSelectQuery = "SELECT * FROM Employee;";
			employeeResults = empSelectStatement.executeQuery(empSelectQuery);

			while (employeeResults.next()) {
				employeeList.add(employeeResults.getString("FullName"));

				employeeList.add(employeeResults.getString("Employee_ID"));

				employeeList.add(employeeResults.getString("Weekly_Hours"));

				int empJobCode = employeeResults.getInt("Job_Code");
				switch (empJobCode) {
				case 0:
					employeeList.add("Hostess");
					break;
				case 1:
					employeeList.add("Server");
					break;
				case 2:
					employeeList.add("Manager");
				}
				employeeList.add(employeeResults.getString("Notes"));
			}

		} catch (Exception ex) {
			System.err.println("Database Call Failure in employeesPanel.");
			System.err
					.println(ex.getClass().getName() + ": " + ex.getMessage());
		}

		GUI.window.setTitle("Employees");
		GUI.centerPanel.setLayout(new BorderLayout());
		GUI.centerPanel.add(new Grids(empHeaders.size(), empHeaders,
				employeeList.size() / empHeaders.size(), employeeList)); // ////
																			// REPLACE
																			// LINE
																			// 109
																			// WITH
																			// THIS
																			// GRID
																			// /////
		// GUI.centerPanel.add(new Label("Table "+tableNum.toString()));
		GUI.leftMenu.setFont(new Font("Arial", Font.PLAIN, 40));
		GUI.leftMenu.add(addemployee(EmpID));
		GUI.leftMenu.add(editemployee(EmpID));
		GUI.leftMenu.add(removeemployee(EmpID));

		GUI.leftPanel.removeAll();
		GUI.leftPanel.add(GUI.leftMenu);
		GUI.bottomPanel.add(GUI.mainMenuBar(empID2));

	}

	private static Component removeemployee(int empID2) {
		JButton button = GUI.newButton("Remove employee", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// GUI.resetPanels();
				ArrayList<String> results = Grids.getEnteredResults();
				String empToRemove = results.get(1);
				// Database call to remove an employee
				Connection c = null;
				try {
					c = SqliteHelper.getConn();
					Statement empDeleteStatement = c.createStatement();
					String empDeleteQuery = "DELETE FROM Employee WHERE Employee_ID = " + empToRemove + ";";				
					empDeleteStatement.executeUpdate(empDeleteQuery);

				} catch (Exception ex) {
					System.err
							.println("Database Call Failure in removeemployee.");
					System.err.println(ex.getClass().getName() + ": "
							+ ex.getMessage());

				}

				Grids.deleteResult(empHeaders);
			}
		});
		return button;
	}

	private static Component editemployee(int empID2) {
		JButton button = GUI.newButton("Edit employee", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// GUI.resetPanels();
				// Obtain the results from the highlighted row
				ArrayList<String> results = Grids.getEnteredResults();
				String empToModify;
				if (results != null) {
					empToModify = results.get(1);
					ArrayList<String> modResults = PopUp
							.runManageEmployee(results);
					// if a parsing error is found pass the arraylist back to
					// runManageEmployee
					while (modResults != null
							&& modResults.get(modResults.size() - 1).equals(
									"ERROR")) {
						modResults = PopUp.runManageEmployee(modResults);
					}
					if (modResults != null) {
						// Edit employee in the database
						Connection c = null;
						try {
							c = SqliteHelper.getConn();

							Statement empUpdateStatement = c.createStatement();
							String newEmpName = modResults.get(0);
							String newEmpPass = modResults.get(1);
							String newEmpHours = modResults.get(2);
							String newEmpJobCode = null;

							switch (modResults.get(3)) {
							case "Hostess":
								newEmpJobCode = "0";
								break;
							case "Server":
								newEmpJobCode = "1";
								break;
							case "Manager":
								newEmpJobCode = "2";
								break;
							}
							String newEmpNotes = modResults.get(4);

							String empUpdateQuery = "UPDATE Employee SET Employee_ID = "
									+ newEmpPass
									+ ", Job_Code = "
									+ newEmpJobCode
									+ ", Notes = '"
									+ newEmpNotes
									+ "', Weekly_Hours = "
									+ newEmpHours
									+ ", FullName = '"
									+ newEmpName
									+ "' WHERE Employee_ID = "
									+ empToModify + ";";

							empUpdateStatement.executeUpdate(empUpdateQuery);

						} catch (Exception ex) {
							System.err
									.println("Database Call Failure in editemployee.");
							System.err.println(ex.getClass().getName() + ": "
									+ ex.getMessage());

						}

						Grids.modifyResult(modResults);
					}
				}
			}
		});
		return button;
	}

	private static Component addemployee(int empID2) {
		JButton button = GUI.newButton("Add employee", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// A blank arraylist is used if a parsing error is found so
				// previous data
				// entered is not lost
				ArrayList<String> blank = new ArrayList<String>();
				for (int i = 0; i < empHeaders.size(); i++) {
					blank.add(null);
				}
				ArrayList<String> results = PopUp.runManageEmployee(blank);
				// if a parsing error is found pass the arraylist back to
				// runManageEmployee
				while (results != null
						&& results.get(results.size() - 1).equals("ERROR")) {
					results = PopUp.runManageEmployee(results);
				}
				if (results != null) {
					// Insert employee into the database
					Connection c = null;
					try {
						c = SqliteHelper.getConn();

						Statement empInsertStatement = c.createStatement();
						String newEmpName = results.get(0);
						String newEmpPass = results.get(1);
						String newEmpHours = results.get(2);
						String newEmpJobCode = null;

						switch (results.get(3)) {
						case "Hostess":
							newEmpJobCode = "0";
							break;
						case "Server":
							newEmpJobCode = "1";
							break;
						case "Manager":
							newEmpJobCode = "2";
							break;
						}
						String newEmpNotes = results.get(4);

						String empInsertQuery = "INSERT INTO Employee VALUES ("
								+ newEmpPass + ", " + newEmpJobCode + ", '"
								+ newEmpNotes + "', " + newEmpHours + ", '"
								+ newEmpName + "');";

						empInsertStatement.executeUpdate(empInsertQuery);

					} catch (Exception ex) {
						System.err
								.println("Database Call Failure in addemployee.");
						System.err.println(ex.getClass().getName() + ": "
								+ ex.getMessage());

					}

					Grids.addNewResult(results, empHeaders);
				}
			}
		});
		return button;
	}

	static JButton menuItemsButton(int empID2) {
		JButton button = GUI.newButton("Menu Items", 40);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.resetPanels();
				GUI.closeFrame();
				menuItemsPanel(empID2);
			}
		});
		return button;
	}

}
